<template>
  <div>
    <UploadExcel :on-success="handleSuccess" />
  </div>
</template>

<script>
import { formatExcelDate } from '@/utils'
import { importEmployeeApi } from '@/api/employees'
export default {
  name: 'Import',
  data() {
    return {}
  },
  computed: {},
  created() {},
  methods: {
    async importEmployee(data) {
      await importEmployeeApi(data)
    },
    handleSuccess({ results, header }) {
      this.importEmployee(this.transExcel(results))

      this.$notify({
        title: '成功',
        message: '导入新员工数据成功!',
        type: 'success'
      })
      this.$router.push('/employees')
    },
    transExcel(results) {
      const mapInfo = {
        入职日期: 'timeOfEntry',
        手机号: 'mobile',
        姓名: 'username',
        转正日期: 'correctionTime',
        工号: 'workNumber',
        部门: 'departmentName',
        聘用形式: 'formOfEmployment'
      }
      var newResults = results.map((item) => {
        // console.log(item)
        const newObj = {}
        const zhKey = Object.keys(item)
        // console.log(zhKey)
        zhKey.forEach((value) => {
          const enKey = mapInfo[value]
          // console.log(enKey)
          if (enKey === 'timeOfEntry' || enKey === 'correctionTime') {
            newObj[enKey] = formatExcelDate(item[value])
          } else {
            newObj[enKey] = item[value]
          }
        })
        return newObj
      })
      return newResults
    }
  }
}
</script>

<style lang="less" scoped></style>
